package com.chixing.day02;

/**
 * @author Y2014188432
 */
public class Homework {
    public static void main(String[] args) {
        int[] a1 = {10,47,65,68,71,83,92};
        int[] a2 = {10,47,65,69,70,71,83,92};
        sort(a1);
        System.out.println(getMedian(a1));
        sort(a2);
        System.out.println(getMedian(a2));
    }

    /**
     * 排序数组（ 递增 ）
     * @param s 待排序数组
     */
    public static void sort(int[] s){
        for (int i = 0; i < s.length; i++) {
            for (int j = i; j < s.length; j++) {
                if(s[i] > s[j]){
                    int temp = s[i];
                    s[i] = s[j];
                    s[j] = temp;
                }
            }
        }
    }




    /**
     * 获取有序数组的中位数
     * @param s 有序数组
     * @return 中位数
     */
    public static int getMedian(int[] s){
        if(s.length %2 == 0){
            return (s[s.length / 2 - 1] + s[s.length / 2]) / 2;
        }else{
            return s[s.length / 2];
        }
    }
}
